import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../../providers/order_provider.dart';

class UserScore extends StatefulWidget {
  const UserScore({Key? key}) : super(key: key);

  @override
  _UserScoreState createState() => _UserScoreState();
}

class _UserScoreState extends State<UserScore> {
  bool useIntegral = false;

  @override
  Widget build(BuildContext context) {
    useIntegral = Provider.of<OrderProvider>(context, listen: false).useIntegral;
    var order = Provider.of<OrderProvider>(context).order;
    var scoreText = useIntegral
      ? "剩余积分："+(order['userInfo']['integral'] - 10).toString()
      : "当前积分："+order['userInfo']['integral'].toString();

    return Container(
      margin: const EdgeInsets.all(10),
      padding: const EdgeInsets.all(10),
      height: 50,
      decoration: BoxDecoration(
        border: Border.all(width: 1, color: Colors.grey.shade300)
      ),
      child: Flex(
        direction: Axis.horizontal,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          const Text('积分抵扣'),
          Row(
            children: [
              Text(
                scoreText,
                style: const TextStyle(color: Colors.grey)
              ),
              Checkbox(
                value: useIntegral, 
                onChanged: (bool? val) {
                  Provider.of<OrderProvider>(context, listen: false).setIntegral(val!);
                  Provider.of<OrderProvider>(context, listen: false).setOrderPrice();
                  setState(() {
                    useIntegral = val;
                  });
                }
              )
            ],
          )
        ],
      ),
    );
  }
}